<?php

/**
 * This is the model class for table "{{actions}}".
 *
 * The followings are the available columns in table '{{actions}}':
 * @property integer $actions_id
 * @property integer $user_id
 * @property string $user_name
 * @property integer $news_id
 * @property integer $action_time
 * @property string $action_type
 * @property string $read_time
 *
 * The followings are the available model relations:
 */
class Actions extends CActiveRecord
{
	public $ncMerge = array();
	/**
	 * Returns the static model of the specified AR class.
	 * @return Actions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{actions}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, user_name, news_id, action_time, read_time', 'required'),
			array('user_id, news_id, action_time', 'numerical', 'integerOnly'=>true),
			array('user_name', 'length', 'max'=>50),
			array('action_type', 'length', 'max'=>5),
			array('read_time', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('actions_id, user_id, user_name, news_id, action_time, action_type, read_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'actions_id' => 'Actions',
			'user_id' => 'User',
			'user_name' => 'User Name',
			'news_id' => 'News',
			'action_time' => 'Action Time',
			'action_type' => 'Action Type',
			'read_time' => 'Read Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('actions_id',$this->actions_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('news_id',$this->news_id);
		$criteria->compare('action_time',$this->action_time);
		$criteria->compare('action_type',$this->action_type,true);
		$criteria->compare('read_time',$this->read_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public static function addActions($news_id,$type){
		$actionsAR = new Actions;
		$actionsAR->user_id = Yii::app()->user->id;
		$actionsAR->news_id = $news_id;
		$actionsAR->user_name = Yii::app()->user->name;
		$actionsAR->action_type = $type;
		$actionsAR->action_time = $actionsAR->read_time = time();
	//	dbg($actionsAR->attributes);
		if($actionsAR->save())return true;
		return false;
	}
	
}